package com.example.yangxu0703.thundermapclient.fragment;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yangxu0703.thundermapclient.R;
import com.example.yangxu0703.thundermapclient.activity.PreViewActivity;
import com.example.yangxu0703.thundermapclient.adapter.PictureAdapter;
import com.example.yangxu0703.thundermapclient.interaction.HoldByDialog;
import com.example.yangxu0703.thundermapclient.iterface.IMGCallback;
import com.example.yangxu0703.thundermapclient.thread.FileDeleteThread;
import com.example.yangxu0703.thundermapclient.thread.SearcherThread;
import com.thundersoft.mm.phone.Wifi.WifiP2pSocket;
import com.truizlop.sectionedrecyclerview.SectionedSpanSizeLookup;

import java.io.File;
import java.util.ArrayList;


public class PictureFragment extends Fragment {

    private View fragmentContent;
    private RecyclerView pic_showView;
    private boolean[][] pic_status;
    private String TAG = "PictureFragment";
    public static ArrayList<ArrayList<String>> all_path;
    private ProgressDialog dialog;
    private PictureAdapter pictureAdapter;
    private TextView action;
    public ArrayList<String> deleteList;
    private boolean chooseMode = false;
    private HoldByDialog holdByDialog, deleteDialog;


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    initRecycle();
                    holdByDialog.dismiss();
                    break;
                case 2:
                    pictureAdapter.setMode(false);
                    pictureAdapter.setImageDate(all_path, pic_status);
                    holdByDialog.dismiss();
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        fragmentContent = inflater.inflate(R.layout.fragment_picture, container, false);
        pic_showView = fragmentContent.findViewById(R.id.RV);
        action = fragmentContent.findViewById(R.id.notifaction_pic);
        holdByDialog = new HoldByDialog(getActivity());
        deleteDialog = new HoldByDialog(getActivity());
        //holdByDialog.showDialog("加载中....");
        Log.i("order", "onCreateView");
        all_path = new ArrayList<>();
        deleteList = new ArrayList<>();
        pic_status = new boolean[100][100];
        notifydata(1, "main");
        return fragmentContent;
    }

    private void initRecycle() {
        if (all_path != null) {
            updateStatus();
            pictureAdapter = new PictureAdapter(all_path, pic_status, getActivity());
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
            SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(pictureAdapter, manager);
            manager.setSpanSizeLookup(lookup);
            pictureAdapter.setOnItemClickListener(new PictureAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int section, int position) {
                    if (!chooseMode) {
                       /* Intent intent = new Intent(getActivity(), PreViewActivity.class);
                        intent.putExtra("SRC-PIC", all_path.get(section).get(position));
                        startActivity(intent);*/
                        Intent intent =new Intent(Intent.ACTION_VIEW);
                        Uri uri= Uri.fromFile(new File(all_path.get(section).get(position)));
                        Log.i("URI",""+uri);
                        intent.setDataAndType(uri,"image/*");
                        startActivity(intent);
                    } else {
                        if (pic_status[section][position]) {
                            pic_status[section][position]=false;
                            deleteList.remove(all_path.get(section).get(position));
                        } else {
                            pic_status[section][position]=true;
                            deleteList.add(all_path.get(section).get(position));
                        }
                        pictureAdapter.notifyDataSetChanged();
                    }
                    Log.i(TAG, "deletelist" + deleteList.size());
                }
            });
            pic_showView.setLayoutManager(manager);
            pic_showView.setAdapter(pictureAdapter);
        }
    }


    private void updateStatus() {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                pic_status[i][j]=false;
            }
        }
    }


    public void update() {
        notifydata(2, "update");
    }

    public void setChooseMode(boolean chooseMode) {
        Log.i(TAG, "setChooseMode" + chooseMode);
        this.chooseMode = chooseMode;
        updateStatus();
        pictureAdapter.setMode(chooseMode);
        Log.i(TAG, "size---" + pic_status.length+ "all_path" + all_path.size());
        pictureAdapter.setImageDate(all_path, pic_status);
        // pictureAdapter.notifyDataSetChanged();
    }

    public void deletefile() {
        Log.i("delete", "deletefile");
        FileDeleteThread thread = new FileDeleteThread(deleteList);
        thread.setFinishCallback(new FileDeleteThread.FinishCallback() {
            @Override
            public void finishback(boolean status) {
                if (status) {
                    Log.i("delete", "finishback");
                    notifydata(2, "delete");
                    deleteList.clear();
                }
            }
        });
        thread.start();
    }

    public void notifydata(final int choose, String name) {
        Log.i(TAG, "notifydata" + "----" + name);
        SearcherThread searcherThread = new SearcherThread(WifiP2pSocket.path, 1);
        searcherThread.setIMGCallBcak(new IMGCallback() {
            @Override
            public void sortPicBcak(ArrayList<ArrayList<String>> url) {
                all_path = url;
                Log.i(TAG, url.size() + "------" + url);
                handler.sendEmptyMessage(choose);
            }

        });
        searcherThread.start();
    }
}
